package es.us.lsi.aws.api.server.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import twitter4j.auth.AccessToken;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import es.us.lsi.aws.api.model.SocialAccount;
import es.us.lsi.aws.api.model.TwitterSocialAccount;

public class DAOSocialAccount {

	public static void addAccount(SocialAccount socialAcc) {
		DatastoreService dataStore = DatastoreServiceFactory
				.getDatastoreService();
		Entity ent = new Entity("Account");
		if (socialAcc instanceof TwitterSocialAccount) {
			String token = ((TwitterSocialAccount) socialAcc).getToken();
			String tokenSecret = ((TwitterSocialAccount) socialAcc)
					.getTokenSecret();
			ent.setProperty("name", socialAcc.getName());
			ent.setProperty("socialNetwork", socialAcc.getSocialNetwork());
			ent.setProperty("token", token);
			ent.setProperty("tokenSecret", tokenSecret);
		}
		dataStore.put(ent);
	}

	public static SocialAccount getAccount(String name, String type) {
		DatastoreService dataStore = DatastoreServiceFactory
				.getDatastoreService();
		Query q = new Query("Account");
		Filter filter1 = new FilterPredicate("name", FilterOperator.EQUAL, name);
		Filter filter2 = new FilterPredicate("socialNetwork", FilterOperator.EQUAL, type);
		Filter filter = CompositeFilterOperator.and(filter1,filter2);
		
		q.setFilter(filter);

		Entity accountEntity = dataStore.prepare(q).asSingleEntity();
		if (type.equals("Twitter")) {
			TwitterSocialAccount res = new TwitterSocialAccount();
			if (accountEntity != null) {
				res.setName(name);
				res.setSocialNetwork(type);
				String token = (String) accountEntity.getProperty("token");
				String tokenSecret = (String) accountEntity
						.getProperty("tokenSecret");
				res.setToken(token);
				res.setTokenSecret(tokenSecret);
				
				return res;
			}
		}

		return null;

	}

	public static List<SocialAccount> getAllAccounts() {
		List<SocialAccount> lstSocialAccounts = new ArrayList<SocialAccount>();

		DatastoreService dataStore = DatastoreServiceFactory
				.getDatastoreService();
		Query q = new Query("Account");

		Iterator<Entity> iEntity = dataStore.prepare(q).asIterator();
		while (iEntity.hasNext()) {
			Entity ent = iEntity.next();
			SocialAccount account = new SocialAccount();
			account.setName((String) ent.getProperty("name"));
			account.setSocialNetwork((String) ent.getProperty("socialNetwork"));
			lstSocialAccounts.add(account);
		}

		return lstSocialAccounts;
	}
}
